import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

class test{

	static P1 jp = new P1();
	static stack sta = new stack();
	static stack res = new stack();
	static stack op = new stack();
	static int mid=0;
	static String str="";

	public static void add(int x){
		str=str+x;
		mid=mid*10+x;
		jp.setText(str);
	} 
	public static void add(String s){
		str+=s;
		//System.out.println("\t\t\t"+mid);
		sta.push(mid);
		mid=0;
		jp.setText(str);
		if(s!="=") sta.push(s);
		else thanOK(); 
	}
	private static void thanOK(){
		String s1=null;	
		while((s1=sta.pop())!="") if(!isOP(s1)) res.push(s1);
		while((s1=op.pop())!="") res.push(s1);
		res.printStack();
		sta.printStack();
		result();
	}
	private static boolean isOP(String s){
		if(s=="+" || s=="-" || s=="*" || s=="/") {
			String t;
			if((t=op.pop())=="") op.push(s);
			else if((s=="-" || s=="+") && (t=="*" || t=="/")){
				res.push(t);
				op.push(s);
			}
			else {
				op.push(t);
				op.push(s);
			}
			return true;
		}
		else return false;
	}

	private static void result(){
		String s,s1,s2;
		while((s=res.pop())!="") {
			if(isop(s)) sta.push(s);
			else {
				if(!isop(s2=sta.pop())) {
					s1=s;s=s2;
					s2=oper(sta.pop(),s,s1);
					if(!isop(s=sta.pop())) sta.push(oper(sta.pop(),s,s2));
					else{
						sta.push(s);
						sta.push(s2);
					}
					continue;
				}
				else {
					sta.push(s2);
					s1=res.pop();
				}				
				if(!isop(s1)) {
					s2=oper(sta.pop(),s,s1);
					if(!isop(s=sta.pop())) sta.push(oper(sta.pop(),s,s2));
					else{
						sta.push(s);
						sta.push(s2);
					}
				}
				else {
					sta.push(s);
					sta.push(s1);
				}
			}
		}
		str=sta.pop();
		jp.setText(str);
		clear();
	}
	private static boolean isop(String s){
		if(s=="+" || s=="-" || s=="*" || s=="/") return true;
		else return false;
	}
	private static String oper(String s,String s1,String s2){

		int a=0,b=0,r=0;
		try
		{
			a=Integer.parseInt(s1);
			b=Integer.parseInt(s2);
		}
		catch(Exception e){
			jp.setText("计算器内部错误！");
		}
		switch(s){
			case "+":r=a+b;
				break;
			case "-":r=a-b;
				break;
			case "*":r=a*b;
				break;
			case "/":r=a/b;
				break;
		}
		System.out.println("res: "+r);
		return r+"";
	}

	public static void clear(){
		sta.clear();
		res.clear();
		mid=0;
		op.clear();
		str="";
	}

	public static void main(String[] args){
		JFrame f = new JFrame("科学计算器");
		//P1 p1 = new P1();
		P2 p2 =new P2();
		P3 p3 = new P3();
				
		f.add(p3,BorderLayout.SOUTH);
		f.add(test.jp,BorderLayout.NORTH);
		f.add(p2,BorderLayout.CENTER);
		//f.pack();
		f.setSize(250,230);
		f.setVisible(true);
	}
}



/*
while((s=res.pop())!="") {
			if(isop(s)) sta.push(s);
			else {
				if(!isop(s2=sta.pop())) {
					s1=s;s=s2;
					s2=oper(sta.pop(),s,s1);
					if(!isop(s=sta.pop())) sta.push(oper(sta.pop(),s,s2));
					else{
						sta.push(s);
						sta.push(s2);
					}
					continue;
				}
				else {
					s1=res.pop();
					sta.push(s2);
				}				
				if(!isop(s1)) {
					s2=oper(sta.pop(),s,s1);
					if(!isop(s=sta.pop())) sta.push(oper(sta.pop(),s,s2));
					else{
						sta.push(s);
						sta.push(s2);
					}
				}
				else {
					sta.push(s);
					sta.push(s1);
				}
			}
		}
*/